package com.code.leetcode.before50;

/**
 * @author codeDorado
 * @version 1.0
 * @date 2020/7/22 12:25
 */
public class The42 {
    public static int trap(int[] height) {

        if (height == null || height.length < 1) return 0;

        int max = 0, index = 0;
        for (int i = 0; i < height.length; i++) {
            if (height[i] > max) {
                max = height[i];
                index = i;
            }
        }
        int left = 0 ,right = height.length - 1, sum = 0 ,temp = 0;
        while (left < index) {
            if (height[left] > height[temp]) {
                temp = left;
            } else {
                sum += (height[temp] - height[left]);
            }
            left ++;
        }
        temp = height.length - 1;
        while (right > index) {
            if (height[right] > height[temp]) {
                temp = right;
            } else {
                sum += (height[temp] - height[right]);
            }
            right--;
        }
        return sum;
    }

    public static void main(String[] args) {
        System.out.println(trap(new int[]{0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}));
    }

}
